<template>
    <div class="instrumentWrapper">
        <cardBox class="left-wrapper" title="厂家仪器" icon="&#xe67b;" isOther>
            <template #right>
                <el-select v-model="factoryId" placeholder="请选择" size="mini" style="width:160px" @change="handleChangeFactory">
                    <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.factoryName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </template>
            <treeMenu :treeData="treeList" name="name" nodeKey="id" expandALL ref="treeMenu" class="tree-menu" @menuLeftClick="menuLeftClick" isRightMenu isInstrument @addMenuItem="handleAddMenu" 
            @editMenuItem="handleEditMenu" @delMenuItem="handleDelMenu" isManufacturer/>
        </cardBox>
        <div class="right">
            <el-card>
                <div slot="header" class="card-title">
                    <div>仪器参数</div>
                    <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAddParams()" v-if="showRoleAdd">新增参数</el-button>
                </div>
                <el-table :data="paramList">
                    <el-table-column align="center" label="参数" prop="param"/>
                    <el-table-column align="center" label="参数名称" prop="parameterName"/>
                    <el-table-column align="center" label="单位" prop="unit"/>
                    <el-table-column  label="操作" align="center">
                        <template slot-scope="scope">
                        <el-link type="primary"  @click="handleAddParams(scope.row)" :underline="false"   icon="el-icon-edit" v-if="showRoleEdit">编辑</el-link>
                        <el-link type="danger"  plain @click="handleDelParams(scope.row)" icon="el-icon-delete" style="margin-left: 12px;" :underline="false" v-if="showRoleDel">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>

            <el-card>
                <div slot="header" class="card-title">
                    <div>仪器分量</div>
                    <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAddApparatus()" v-if="showRoleAdd">新增分量</el-button>
                </div>
                 <el-table :data="apparatusList">
                    <el-table-column align="center" label="分量"  prop="comp" />
                    <el-table-column align="center" label="分量名称" prop="compname"/>
                    <el-table-column align="center" label="类型" prop="valueType">
                    <template slot-scope="scope">
                        <span v-if="scope.row.valueType == 0">初始值</span>
                        <span v-if="scope.row.valueType == 1">中间值</span>
                        <span v-if="scope.row.valueType == 2">结果值</span>
                    </template>
                    </el-table-column>
                    <el-table-column align="center" label="上限" prop="upperLimit">
                        <template slot-scope="scope">
                            <span class="greenText">{{ scope.row.upperLimit }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="下限" prop="lowerLimit">
                        <template slot-scope="scope">
                            <span class="redText">{{ scope.row.lowerLimit }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="单位" prop="unit" />
                    <el-table-column align="center" label="精度" prop="precision" />
                    <el-table-column align="center" label="公式" prop="calculationFormula" show-overflow-tooltip>
                    
                    </el-table-column>
                    <el-table-column align="center" label="是否使用">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.sign == 1">已使用</el-tag>
                        <el-tag type="danger" size="small" v-if="scope.row.sign != 1">未使用</el-tag>
                    </template>
                    </el-table-column>
                    <el-table-column align="center" label="是否收敛">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.comP_CON == 1">是</el-tag>
                        <el-tag type="danger" size="small" v-if="scope.row.comP_CON != 1">否</el-tag>
                    </template>
                    </el-table-column>
                    <el-table-column align="center" label="显示顺序" prop="inx"> 
                    </el-table-column>
                    <el-table-column align="center" label="计算顺序" prop="indCalc">
                    </el-table-column>
                    <el-table-column align="center" label="默认值" prop="defaultVal">
                    </el-table-column>
                    <el-table-column  label="操作" align="center" width="150px">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="handleAddApparatus(scope.row)" icon="el-icon-edit" :underline="false" v-if="showRoleEdit">编辑</el-link>
                        <el-link type="danger" @click="handleDelApparatus(scope.row)" icon="el-icon-delete" style="margin-left: 12px;" :underline="false" v-if="showRoleDel">删除</el-link>
                    </template>
                    </el-table-column>
                </el-table>
            </el-card>

            <el-card>
                <div slot="header" class="card-title">
                    <div>关联point</div>
                    <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAddPoint()" v-if="showRoleAdd">新增关联</el-button>
                </div>
                <el-table :data="instrmentPointList">
                    <el-table-column align="center" label="POINT" prop="Point"/>
                    <el-table-column align="center" label="名称" prop="PointName"/>
                    <el-table-column align="center" label="取值范围" prop="V_D_Range"/>
                    <el-table-column align="center" label="测值选择" prop="V_TypeID"/>
                    <el-table-column align="center" label="自定时刻" prop="V_IsMoment">
                        <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.V_IsMoment == 1 ">是</el-tag>
                        <el-tag type="danger" size="small" v-else>否</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="取值时刻" prop="V_Moment"/>
                    <el-table-column align="center" label="默认值设置" prop="V_IsDefl">
                        <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.V_IsDefl == 1 ">是</el-tag>
                        <el-tag type="danger" size="small" v-else>否</el-tag>
                        </template>
                    </el-table-column> 
                    <el-table-column align="center" label="默认值" prop="V_Defl"/> 
                    <el-table-column  label="操作" align="center" width="150">
                        <template slot-scope="scope">
                        <el-link type="primary"  @click="handleAddPoint(scope.row)" icon="el-icon-edit" :underline="false" v-if="showRoleEdit">编辑</el-link>
                        <el-link type="danger" @click="handleDelPoint(scope.row)" icon="el-icon-delete" style="margin-left: 12px;" :underline="false" v-if="showRoleDel">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>        
        </div>

    <el-dialog 
    title="仪器分量配置" 
    :visible.sync="apparatusDialogVisible" 
    width="80%"
    :before-close="closeDialog"
    class="apparatus-dialog">
    
    <div class="apparatus-form-container">
      <el-form 
        ref="apparatusForm" 
        :model="apparatusForm" 
        :rules="rules" 
        label-width="100px" 
        size="small">
        <div class="form-row">
          <el-form-item label="分量" prop="comp" class="form-item-compact">
            <el-input 
              v-model="apparatusForm.comp" 
              placeholder="分量标识"
              size="small">
            </el-input>
          </el-form-item>
          <el-form-item label="分量名称" prop="compname" class="form-item-compact">
            <el-input 
              v-model="apparatusForm.compname" 
              placeholder="分量名称"
              size="small">
            </el-input>
          </el-form-item>
          <el-form-item label="类型" prop="valueType" class="form-item-compact">
            <el-select v-model="apparatusForm.valueType" placeholder="类型" size="small">
              <el-option label="初始值" :value="0"></el-option>
              <el-option label="中间值" :value="1"></el-option>
              <el-option label="结果值" :value="2"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="上限" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.upperLimit" 
              :precision="apparatusForm.precision || 2"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
          <el-form-item label="下限" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.lowerLimit" 
              :precision="apparatusForm.precision || 2"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
          <el-form-item label="精度" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.precision" 
              :min="0"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="单位" class="form-item-compact">
            <el-input 
              v-model="apparatusForm.unit" 
              placeholder="单位"
              size="small">
            </el-input>
          </el-form-item>
          <el-form-item label="默认值" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.DefaultVal" 
              :precision="apparatusForm.precision || 2"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
          <el-form-item label="显示顺序" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.inx" 
              :min="0"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="是否使用" class="form-item-compact switch-item">
            <el-switch
              v-model="apparatusForm.sign"
              :active-value="1"
              :inactive-value="0"
              size="small">
            </el-switch>
          </el-form-item>
          <el-form-item label="允许为空" class="form-item-compact switch-item">
            <el-switch
              v-model="apparatusForm.canNull"
              :active-value="1"
              :inactive-value="0"
              size="small">
            </el-switch>
          </el-form-item>
          <el-form-item label="是否收敛" class="form-item-compact switch-item">
            <el-switch
              v-model="apparatusForm.comP_CON"
              :active-value="1"
              :inactive-value="0"
              size="small">
            </el-switch>
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="识别方法" class="form-item-compact">
            <el-radio-group v-model="apparatusForm.method" size="small">
              <el-radio label="Extreme" class="compact-radio"></el-radio>
              <el-radio label="auto" class="compact-radio"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="Sigma" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.sigma_threshold" 
              :min="0"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
          <el-form-item label="计算顺序" class="form-item-compact">
            <el-input-number 
              v-model="apparatusForm.indCalc" 
              :min="0"
              size="small"
              controls-position="right"
              class="compact-input-number">
            </el-input-number>
          </el-form-item>
        </div>
        <div class="form-row full-width">
          <el-form-item label="公式" class="formula-item">
            <el-input 
              type="textarea"
              v-model="apparatusForm.calculationFormula" 
              :rows="4"
              placeholder="请输入计算公式"
              resize="none"
              size="small">
            </el-input>
          </el-form-item>
        </div>

      </el-form>
    </div>
    <div class="dialog-footer">
        <el-button size="small" @click="closeDialog" icon="el-icon-circle-close" class="cancel-btn">取消</el-button>
        <el-button type="primary" icon="el-icon-circle-check" @click="handleSaveApparatus" size="small" class="confirm-btn">保存配置</el-button>
    </div>
  </el-dialog>

    <el-dialog title="关联point配置" v-dialogDrag
      :visible.sync="instrmentDialog"
      width="450px">
        <el-form ref="instrmentPointForm" :model="instrmentPointForm" label-width="120px" size="small">
            <el-form-item label="POINT">
              <el-input class="apparatusIpt" v-model="instrmentPointForm.Point" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="POINT名称">
              <el-input class="apparatusIpt" v-model="instrmentPointForm.PointName" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="取值范围">
              <el-input-number class="apparatusIpt" controls-position="right" v-model="instrmentPointForm.V_D_Range" placeholder="请输入"/><span style="margin-left: 10px;">天</span>
            </el-form-item>
            <el-form-item label="测值选择">
              <el-select v-model="instrmentPointForm.V_TypeID" placeholder="请选择">
                <el-option  
                v-for="item in pointTypeSelect"
                :key="item.ID" :label="item.V_TypeEXP" :value="item.V_TypeID"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否自定时刻">
              <el-switch
                v-model="instrmentPointForm.V_IsMoment"
                 active-color="#13ce66" inactive-color="#f0f0f0" :active-value="1" :inactive-value="0">
              </el-switch>
            </el-form-item>
            <el-form-item label="取值时刻" v-if="instrmentPointForm.V_IsMoment == 1">
              <el-select v-model="instrmentPointForm.V_Moment" placeholder="请选择">
                <el-option v-for="i in 24" :key="i" :label="`${i}时`" :value="i"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否默认值设置">
              <el-switch
                v-model="instrmentPointForm.V_IsDefl"
                 active-color="#13ce66" inactive-color="#f0f0f0" :active-value="1" :inactive-value="0">
              </el-switch>
            </el-form-item>
            <el-form-item label="默认值设置" v-if="instrmentPointForm.V_IsDefl == 1">
              <el-input class="apparatusIpt" v-model="instrmentPointForm.V_Defl" placeholder="请输入"/>
            </el-form-item>
        </el-form>
        <div class="dialog-footer">
            <el-button size="small" @click="instrmentDialog = false" icon="el-icon-circle-close" class="cancel-btn">取消</el-button>
            <el-button type="primary" icon="el-icon-check" @click="instrmentPointSubmit" size="small" class="confirm-btn">保存配置</el-button>
        </div>
    </el-dialog>

    <el-dialog title="设备类型/仪器" :visible.sync="menuDialogVisible"
      :before-close="handleCancelMenu" v-dialogDrag width="450px">
        <el-form ref="menuData" :model="menuData" label-width="100px">
            <el-form-item label="厂家选择">
              <el-select v-model="menuData.factoryId" placeholder="请选择所属厂家">
                <el-option
                  v-for="item in options"
                  :key="item.id" 
                  :label="item.factoryName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="level == 1" label="设备类型名称">
              <el-input class="aratusItem" v-model="menuData.typeName" placeholder="请输入"/>
            </el-form-item>
            <el-form-item v-if="level == 2" label="设备仪器名称">
              <el-input class="aratusItem" v-model="menuData.instrumentModelName" placeholder="请输入"/>
            </el-form-item>
        </el-form>
        <div class="btnGroup">
            <el-button size="small" @click="handleCancelMenu" icon="el-icon-circle-close">取消</el-button>
            <el-button type="primary" icon="el-icon-circle-check" @click="handleSaveMenu" size="small">保存</el-button>
        </div>
    </el-dialog>


    </div>
</template>
<script>
import treeMenu from '@/components/base/treeMenu.vue'
import {GetInstrumentObj,GetInstrumentCOMPByModeId,GetParameterByModeId,InstrumentPOINT,AddInstrumentParameter,EditInstrumentParameter,GetInstrumentCOMPDelParame,EditInstrumentCOMP,AddInstrumentCOMP,GetInstrumentCOMPDelModeId,getAllInstrumentPOINTType,InstrumentPOINTEdit,InstrumentPOINTAdd,InstrumentPOINTDel,EditInstrumentType,AddInstrumentType,EditInstrumentModel,AddInstrumentModel,DelInstrumentType,DelInstrumentModel} from '@/api/https'
import cardBox from "@/components/base/cardBox.vue";
export default({
    components:{cardBox,treeMenu},
    props:{
        options:{
            type:Array,
            default:()=>{
                return []
            }
        },
        showRoleAdd:{
            type:Number,
            default:0
        },
        showRoleDel:{
            type:Number,
            default:0
        },
        showRoleEdit:{
            type:Number,
            default:0
        }
    },
    mounted(){
        if(this.options.length > 0){
            this.factoryId = this.options[0].id
            this.getInstrument(true)
        }
        this.getInstrumentPointSelect()
    },
    data() {
        return {
            treeList:[],
            factoryId:'',
            modelId:'',
            apparatusList:[],
            paramList:[],
            instrmentPointList:[],
            paramForm:{},
            paramDialogVisible:false,
            editParams:false,
            apparatusDialogVisible:false,
            apparatusForm:{},
            apparatusEdit:false,
            pointTypeSelect:[],
            instrmentPointForm:{},
            instrmentEdit:false,
            instrmentDialog:false,
            menuDialogVisible:false,
            menuData:{},
            level:'',
            rules: {
                comp: [
                { required: true, message: '请输入分量标识', trigger: 'blur' }
                ],
                compname: [
                { required: true, message: '请输入分量名称', trigger: 'blur' }
                ],
                valueType: [
                { required: true, message: '请选择类型', trigger: 'change' }
                ]
            }
        }
    },
    methods:{
        getInstrument(isReload) {
            GetInstrumentObj({
                FactoryID:this.factoryId
            }).then(res=>{
                if (res.isSuccess) {
                    this.treeList = res.data || [];
                    if(isReload){
                        if(res.data && res.data.length > 0){
                            this.findLeafNodes(res.data);
                            this.$nextTick(()=>{
                                this.$refs.treeMenu.$refs.tree.setCurrentKey(this.modelId);
                                this.getApparatus()
                            })
                        }
                    }
                }
            });
        },
        findLeafNodes(data) {
            if (data[0].children == null || data[0].children.length === 0){
                this.modelId = data[0].id
                return
            }else{
                this.findLeafNodes(data[0].children);
            }
        },
        getApparatus() {
            this.getList1()
            this.getList2()
            this.getList3()
        },
        getList1(){
            GetParameterByModeId(this.modelId).then(res=>{
                if (res.isSuccess) {
                    this.paramList = res.data || [];
                }
            });
        },
        getList2(){
            GetInstrumentCOMPByModeId(this.modelId).then(res=>{
                if (res.isSuccess) {
                    this.apparatusList = res.data || [];
                }
            });
        },
        getList3(){
            InstrumentPOINT({
                InstrumentModelID:this.modelId
            }).then(res=>{
                if(res.isSuccess){
                    this.instrmentPointList = res.data || []
                }
            })
        },
        menuLeftClick(data){
            this.modelId = data.id
            this.getApparatus()
        },
        handleAddParams(data) {
            if(this.modelId == ''){
                this.$message.error('请先选择仪器目录！')
                return false
            }
            if(data){
                this.editParams = true
                this.paramForm = {...data}
            }else{
                this.editParams = false
                this.paramForm = {
                    instrumentModelID:this.modelId,
                    id: 0,
                    param: "",
                    parameterName: "",
                    unit: ""
                };
            }
            this.paramDialogVisible = true;
        },
        closeDialog(){
            this.paramDialogVisible = false;
            this.editParams = false
            this.paramForm = {}
            this.apparatusDialogVisible = false;
            this.apparatusEdit = false
            this.apparatusForm = {}

        },
        handleSaveParams(){
            this.$refs['paramForm'].validate((valid) => {
                if (valid) {
                    if (this.editParams) {
                        EditInstrumentParameter(this.paramForm).then(res=>{
                            if (res.isSuccess) {
                                this.$message.success("操作成功！");
                                this.closeDialog()
                                this.getList1();
                            }else{
                                this.$message.error(res.errorMessage);
                            }
                        });
                    } else {
                        AddInstrumentParameter(this.paramForm).then(res=>{
                            if (res.isSuccess) {
                                this.$message.success("操作成功！");
                                this.closeDialog()
                                this.getList1();
                            }else{
                                this.$message.error(res.errorMessage);
                            }
                        });
                        
                    }
                } else {
                    return false;
                }
            });
            
        },
        handleDelParams(rowData){
            this.$confirm(`确认删除该 ${rowData.parameterName} 仪器参数？`,'提示',{
                type:'warning'
            }).then(() => {
                GetInstrumentCOMPDelParame(rowData.id).then(res=>{
                    if (res.isSuccess) {
                        this.$message.success("删除成功");
                        this.getList1()
                    }else{
                        this.$message.error(res.errorMessage);
                    }
                })
            }).catch(() => {});  
        },
        handleAddApparatus(data) {
            if(this.modelId == ''){
                this.$message.error('请先选择仪器目录！')
                return false
            }

            if(data){
                this.apparatusForm = {...data}
                this.apparatusEdit = true
            }else{
                this.apparatusEdit = false
                this.apparatusForm = {
                    instrumentModelID:this.modelId,
                    id: 0,
                    comp: "",
                    compname: "",
                    unit: "",
                    upperLimit: 0,
                    lowerLimit: 0,
                    valueType: 0,
                    comP_CON: 0,
                    precision: 0,
                    calculationFormula: "",
                    sign: 0,
                    inx:1,
                    indCalc:1
                };
            }
            this.apparatusDialogVisible = true;
        },
        handleSaveApparatus() {
            
            this.$refs['apparatusForm'].validate((valid) => {
                if (valid) {
                    if (this.apparatusEdit) {
                        EditInstrumentCOMP(this.apparatusForm).then(res=>{
                            if (res.isSuccess) {
                                this.closeDialog()
                                this.getList2()
                            }else{
                                this.$message.error(res.errorMessage)
                            }
                        });
                    } else {
                        AddInstrumentCOMP(this.apparatusForm).then(res=>{
                            if (res.isSuccess) {
                                this.closeDialog()
                                this.getList2()
                            }else{
                                this.$message.error(res.errorMessage)
                            }
                        });
                        
                    }
                } else {
                    return false;
                }
            });
            
        },
        handleDelApparatus(rowData){
            this.$confirm(`确认删除 ${rowData.compname} 分量参数？`,'提示',{
                type:'warning'
            }).then(() => {
                GetInstrumentCOMPDelModeId(rowData.id).then(res=>{
                    if (res.isSuccess) {
                        this.$message.success("删除成功");
                        this.getList2()
                    }else{
                        this.$message.error(res.errorMessage);
                    }
                })
            }).catch(() => {});  
        },
        getInstrumentPointSelect(){
            getAllInstrumentPOINTType().then(res=>{
                if(res.isSuccess){
                    this.pointTypeSelect = res.data || []
                }
            })
        },
        handleAddPoint(data){
            if(this.modelId == ''){
               this.$message.error('请先选择仪器目录！')
                return false
            }
            if(data){
                this.instrmentPointForm = {...data}
                this.instrmentEdit = true
            }else{
                this.instrmentPointForm = {
                    InstrumentModelID:this.modelId,
                    ID: 0,
                    Point:"",
                    PointName: "",
                    V_D_Range: 1,
                    V_IsDefl: 0,
                    V_IsMoment: 0,
                    V_TypeID: 1
                }
                this.instrmentEdit = false
            }
            this.instrmentDialog = true
        },
        instrmentPointSubmit(){
            if(this.instrmentEdit){
                InstrumentPOINTEdit(this.instrmentPointForm).then(res=>{
                    if(res.isSuccess){
                        this.instrmentDialog = false
                        this.instrmentEdit = false
                        this.instrmentPointForm = {}
                        this.getList3()
                    } else {
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                InstrumentPOINTAdd(this.instrmentPointForm).then(res=>{
                    if(res.isSuccess){
                        this.instrmentDialog = false
                        this.instrmentEdit = false
                        this.instrmentPointForm = {}
                        this.getList3()
                    } else {
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        },
        handleDelPoint(data){
            this.$confirm(`确认删除名称为 ${data.PointName} point参数？`,'提示',{
                type:'warning'
            }).then(() => {
                InstrumentPOINTDel(data.ID).then(res=>{
                    if(res.isSuccess){
                        this.instrmentDialog = false
                        this.getList3()
                    }
                })
            }).catch(() => {}); 
        },
        handleChangeFactory(val){
            this.modelId = val
            this.getInstrument(true)
        },
        handleAddMenu(data,isEdit,treeNodeLevel){
            if(treeNodeLevel == 1){
                this.menuData = {
                    ...data,
                    factoryId:this.factoryId
                };
                this.menuDialogVisible = true;
                this.level = 1;
            }else{
                this.level = 2
                this.menuData = {
                    id: 0,
                    productTypeID: data.id,
                    factoryId:this.factoryId
                }
                this.menuDialogVisible = true;
            }
        },
        handleEditMenu(data,isEdit,nodeLevel){
            this.isEditMenu = true
            const _data = {
                id: data.id,
                typeName: data.name,
                instrumentModelName: data.name,
                productTypeID: data.productTypeID ? data.productTypeID : null,
                factoryId:data.factoryId ? data.factoryId : this.factoryId
            }
            this.menuData = {..._data}
            this.level = nodeLevel
            this.menuDialogVisible = true;
        },
        handleSaveMenu(){
            if(this.level == 1){
                if(this.isEditMenu){
                    EditInstrumentType(this.menuData).then(res=>{
                        if (res.isSuccess) {
                            this.handleCancelMenu()
                            this.getInstrument(false);
                        }else{
                            this.$message.error(res.errorMessage);
                        }
                    });
                }else{
                    AddInstrumentType(this.menuData).then(res=>{
                        if (res.isSuccess) {
                            this.handleCancelMenu()
                            this.getInstrument(false);
                        }else{
                            this.$message.error(res.errorMessage);
                        }
                    });
                }
            }else if(this.level == 2){
                if(this.isEditMenu){
                    EditInstrumentModel(this.menuData).then(res=>{
                        if (res.isSuccess) {
                            this.handleCancelMenu()
                            this.getInstrument(false);
                        }else{
                        this.$message.error(res.errorMessage);
                        }
                    });
                }else{
                    AddInstrumentModel(this.menuData).then(res=>{
                        if (res.isSuccess) {
                            this.handleCancelMenu()
                            this.getInstrument(false);
                        }else{
                            this.$message.error(res.errorMessage);
                        }
                    });
                }
            }
        },
        handleDelMenu(data,nodeLevel){
            if (nodeLevel == 1) {
                this.$confirm(`确认删除名为[${data.name}]的仪器类型？`,'提示',{
                    type:'warning'
                }).then(() => {
                    DelInstrumentType(data.id).then(res=>{
                        if(res.isSuccess) {
                            this.$message.success("删除成功");
                            this.getInstrument(false);
                        }else{
                            this.$message.error(res.errorMessage);
                        }
                    });
                }).catch(() => {});  
            } else if(nodeLevel == 2) {
                this.$confirm(`确认删除名为[${data.name}]的仪器设备？`,'提示',{
                    type:'warning'
                }).then(() => {
                    DelInstrumentModel(data.id).then(res=>{
                        if (res.isSuccess) {
                            this.$message.success("删除成功");
                            this.getInstrument(false);
                        }else{
                        this.$message.error(res.errorMessage);
                        }
                    });
                }).catch(() => {});  
            }
        },
        handleCancelMenu(){
            this.menuData = {}
            this.level = ''
            this.menuDialogVisible = false;
            this.isEditMenu = false
        }
    }
})
</script>
<style scoped lang="scss">
.instrumentWrapper{
    display: grid;
    grid-template-columns: 340px 1fr;
    height: calc(100vh - 180px);
    margin-top: 10px;
    .left-wrapper{
        width:340px;
        height: calc(100vh - 180px);
        .tree-menu{
            height: calc(100vh - 250px);
            overflow: auto;
        }
    }
    .right{
        height: calc(100vh - 180px);
        margin-left: 10px;
        box-sizing: border-box;
        overflow: auto;
        ::v-deep .el-card{
            margin-bottom: 10px;
        }
    }
}
.card-title{
  font-weight: bold;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}



.apparatus-form-container {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 5px;
}

.apparatus-form-container::-webkit-scrollbar {
  width: 4px;
}

.apparatus-form-container::-webkit-scrollbar-thumb {
  background-color: #c0c4cc;
  border-radius: 2px;
}

.form-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: flex-start;
}

.form-row.full-width {
  display: block;
}

.form-item-compact {
  flex: 1;
  margin-bottom: 10px;
}

.form-item-compact :deep(.el-form-item__label) {
  padding-right: 8px;
  line-height: 32px;
}

.form-item-compact :deep(.el-form-item__content) {
  line-height: 32px;
}

.switch-item :deep(.el-form-item__content) {
  display: flex;
  align-items: center;
  height: 32px;
}

.compact-input-number {
  width: 100%;
}

.compact-input-number :deep(.el-input__inner) {
  text-align: left;
}

.compact-radio {
  margin-right: 8px;
}

.formula-item {
  margin-bottom: 0;
}

.formula-item :deep(.el-form-item__content) {
  line-height: normal;
}

.btn-group {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
  padding-top: 15px;
  border-top: 1px solid #ebeef5;
}
</style>
